<?php
/**
 * @version $Id: trackback.class.php 181 2008-03-11 21:46:28Z mikhail.miguel $
 * @author  Takeshi Kuriyama <kuri@keynext.co.jp>
 */

if( ! defined( 'XOOPS_TRUST_PATH' ) ) die( 'set XOOPS_TRUST_PATH into mainfile.php' );

require_once dirname(dirname(__FILE__)).'/lib/object.php';
require_once dirname(dirname(__FILE__)).'/include/filter/TrackbackFilter.class.php';

if( ! class_exists('d3blogTrackbackObjectBase') ){

class d3blogTrackbackObjectBase extends myXoopsObject {
    function d3blogTrackbackObjectBase($id=null)
    {
        $this->initVar("tid", XOBJ_DTYPE_INT, 0, true);
        $this->initVar("bid", XOBJ_DTYPE_INT, 0, true);
        $this->initVar("blog_name", XOBJ_DTYPE_TXTBOX, null, false, 255);
        $this->initVar("url", XOBJ_DTYPE_TXTBOX, null, false, 150);
        $this->initVar("trackback_url", XOBJ_DTYPE_TXTBOX, null, false, 150);
        $this->initVar("title", XOBJ_DTYPE_TXTBOX, null, false, 255);
        $this->initVar("excerpt", XOBJ_DTYPE_TXTAREA, null, false);
        $this->initVar("direction", XOBJ_DTYPE_INT, 0, false);
        $this->initVar("host", XOBJ_DTYPE_TXTBOX, null, false, 15);
        $this->initVar("tbkey", XOBJ_DTYPE_TXTBOX, null, false, 12);
        $this->initVar("approved", XOBJ_DTYPE_INT, 0, false);
        $this->initVar("created", XOBJ_DTYPE_INT, time(), false);

        if ( is_array ( $id ) )
            $this->assignVars ( $id );
    }

    function &getStructure($type='s')
    {
        $ret =& parent::getStructure($type);

        return $ret;
    }

	function isApproved() {
		return $this->getVar('approved');
	}

}

class d3blogTrackbackObjectHandlerBase extends myXoopsObjectHandler
{
    var $mydirname_; 
    var $filter_; 

    function d3blogTrackbackObjectHandlerBase($db,$classname=null) { 
        parent::myXoopsObjectHandler($db,$classname); 
        $this->mydirname_ = '';   
    } 
    
    function getDefaultCriteria() {
        // get a default trackback criteria thru filter
        $this->filter_ = call_user_func(array($this->mydirname_.'TrackbackFilter', 'getInstance'));
        $criteria =& $this->filter_->getDefaultCriteria();
        return $criteria;           
    }

    function getCriteria() {
        // get trackback criteria thru filter
        $this->filter_ = call_user_func(array($this->mydirname_.'TrackbackFilter', 'getInstance'));
        $criteria =& $this->filter_->getCriteria();
        return $criteria;           
    }

    function getTrackback($bid, $direction=null, $as_object=false) {
        $myModule = call_user_func(array($this->mydirname_, 'getInstance'));
        $return = array();
        
        if(empty($bid))
            return $return;

        $this->filter_ = call_user_func(array($this->mydirname_.'TrackbackFilter', 'getInstance'));        
        $criteria = $this->filter_->getCriteria(); 

        $criteria->add(new criteria('bid', intval($bid))); 

        if($direction)
            $criteria->add(new criteria('direction', intval($direction)));            
        else
            $criteria->add(new criteria('direction', '0', '<>'));
        
        $objs =& $this->getObjects($criteria);
     
        if(count($objs)) {
            foreach($objs as $obj) {
                $direction = $obj->getVar('direction')==1? 'outbound' : 'inbound';
                if(!$as_object) {
                    $return[$direction][$obj->getVar('tid')] = $obj->getStructure();
                } else {
                	$return[$direction][$obj->getVar('tid')] = $obj;
                }
            }
        }
        return $return;  
    }

    function send(&$obj, $form) {
    }

    function receive(&$entry, $obj=null) {
    }

    function getByTbkey($id) {
    	$myts =& d3blogTextSanitizer:: getInstance();
        $criteria = new criteriaCompo(new criteria('tbkey', $myts->addSlashes($id)));    
        return $this->getOne($criteria);
    }

    // purge old trackback key. works every 10 times by default
    function garbageTrackbackKey($per=10, $expiration=86400){
        $per=intval($per);
        if(rand(0,100) >  $per){
            return null ;
        }
        $expiration = inyval($expiration);
        $sql = sprintf('delete from %s where direction=\'0\' and created<%d',
                       $this->db->prefix($this->mydirname_.'_trackback'), time()-$expiration ); // default 1 day
        $this->db->query($sql);
    }
    function validateUrl($url) {
        $arr = parse_url( $url );
        if( $arr['scheme']=='http' && $arr['host'] && $arr['path'] ){
            return true ;
        }else{
            return false ;
        }        
    }

    function insert(&$obj, $force=false)
    {
        return parent::insert($obj, $force);
    }

    function delete(&$obj, $force=false) {
        return parent::delete($obj, $force);
    }

    function deletes($criteria = null)
    {
        parent::deletes($criteria);
    }

}
}

if( ! class_exists($object_class_name) ) {
require dirname(dirname(__FILE__)).'/include/filter/TrackbackFilter.class.php';
eval('
class '. $object_class_name .' extends d3blogTrackbackObjectBase {
    function '. $object_class_name .'($id=null) {
        $this->d3blogTrackbackObjectBase();
    }
    function getTableInfo() {
        $tinfo = new myTableInfomation("'.$mydirname.'_trackback", "tid");
        return ($tinfo);
    }
}
');
eval('
class '. $object_class_name .'Handler extends d3blogTrackbackObjectHandlerBase {
    var $mydirname_;
    function '. $object_class_name .'Handler($db) {
        parent::myXoopsObjectHandler($db, "'. $object_class_name .'");
        $this->mydirname_ = "'.$mydirname.'";
    }
}
');
}

?>